package com.hshx.config;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

//@Configuration
public class CreateAndInit {
//    public static final String EXCHANGE = "direct"; // 交换空间名称//
//    public static final String ROUTINGKEY = "rk"; // 设置路由key
    /**
     * DirectExchange 直连模式
     * @return
     */
//    @Bean
//    public DirectExchange directExchange() {
//        // 要创建的交换机
//        // 持久化(存硬盘，重启还在，默认true)
//        // 自动删除(当所有绑定队列都不在使用时删除交换机)
//        return new DirectExchange(EXCHANGE, true, true);
//    }
//    @Bean
//    public Queue queue11() {
//        // 要创建的队列
//        // 持久化(保存硬盘，重启存在，默认true)
//        // 排他(只有创建它的连接可用，连接关闭时无论队列中有没有消息会删除队列)
//        // 自动删除(当队列中有消息时，无论是否排他，关闭连接都不会删除队列，此时消费者消费完消息后再断开消费者，队列会被自动删除，删除后队列消息也丢失)
//        // args参数设置队列最多5条消息(保留最后的5条消息，前面的全部丢失)
//        Map<String,Object> args = new HashMap<>();
//        args.put("x-max-length",5);
//        return new Queue("q11",true,false,false,args);
//    }
//    @Bean
//    public Queue queue12() {
//        // args参数设置队列过期时间10秒，10秒后所有消息清空
//        Map<String,Object> args = new HashMap<>();
//        args.put("x-message-ttl",10000);
//        return new Queue("q12", true,false,false,args);
//    }
//    @Bean
//    public Binding bindingExchangeQueue11(DirectExchange exchange, Queue queue11) {
//        return BindingBuilder.bind(queue11).to(exchange).with(ROUTINGKEY);
//    }
//    @Bean
//    public Binding bindingExchangeQueue12(DirectExchange exchange, Queue queue12) {
//        return BindingBuilder.bind(queue12).to(exchange).with(ROUTINGKEY);
//    }




    //死信交换机
    @Bean
    public DirectExchange deadExchange(){
        return new DirectExchange("deadExchange",true,false);
    }

    //死信队列
    @Bean
    public Queue deadQueue(){
        Map<String,Object> args = new HashMap<>();
        args.put("x-dead-letter-exchange","deadExchange");
        args.put("x-dead-letter-routing-key","deadRouting");
        return new Queue("dead.Queue",true,false,false,args);
    }
    //死信队列绑定
    @Bean
    public Binding bindingExchangeQueue13() {
        return BindingBuilder.bind(deadQueue())
                .to(deadExchange()).with("deadRouting");
    }
//    @Bean
//    public Queue queue11(){
//        Map<String,Object> args = new HashMap<>();
//        args.put("x-max-length",5);	//超过5个的进入死信队列
//        args.put("x-dead-letter-exchange","deadExchange");
//        args.put("x-dead-letter-routing-key","deadRouting");
//        return new Queue("q11",true,false,false,args);
//    }

//    @Bean
//    public Queue queue12(){
//        Map<String,Object> args = new HashMap<>();
//        args.put("x-message-ttl",10000); //超过10秒未消费的进入死信队列
//        args.put("x-dead-letter-exchange","deadExchange");
//        args.put("x-dead-letter-routing-key","deadRouting");
//        return new Queue("q12",true,false,false,args);
//    }

//    @Bean
//    public Binding bindingExchangeQueue11(DirectExchange directExchange, Queue queue11) {
//        return BindingBuilder.bind(queue11)
//                .to(directExchange).with("flushPosition");
//    }

//    @Bean
//    public Binding bindingExchangeQueue12(DirectExchange directExchange, Queue queue12) {
//        return BindingBuilder.bind(queue12)
//                .to(directExchange).with(ROUTINGKEY);
//    }

    /**
     * FanoutExchange 广播模式
     * @return
     */
//    @Bean
//    public FanoutExchange getFanoutExchange(){
//        return new FanoutExchange("fanout");
//    }
//    @Bean
//    public Queue queue21() {
//        return new Queue("q21");
//    }
//    @Bean
//    public Queue queue22() {
//        return new Queue("q22");
//    }
//    @Bean
//    public Binding bindingExchangeQueue21(FanoutExchange exchange, Queue queue21) {
//        return BindingBuilder.bind(queue21).to(exchange);
//    }
//    @Bean
//    public Binding bindingExchangeQueue22(FanoutExchange exchange, Queue queue22) {
//        return BindingBuilder.bind(queue22).to(exchange);
//    }

    /**
     * TopicExchange
     * RabbitMQ使用通配符实现topic模式：*.topic.#
     * "*"用于匹配一个单词，比如"a","abc"等；
     * "#"用于匹配0个或者多个单词，比如"", "abc", "abc.def"等
     * @return
     */
//    @Bean
//    public TopicExchange getTopicExchange(){
//        return new TopicExchange("topic");
//    }
//    @Bean
//    public Queue queue31() {
//        return new Queue("q31");
//    }
//    @Bean
//    public Queue queue32() {
//        return new Queue("q32");
//    }
//    @Bean
//    public Binding bindingExchangeQueue31(TopicExchange exchange, Queue queue31) {
//        return BindingBuilder.bind(queue31).to(exchange).with("#.log.*");
//    }
//    @Bean
//    public Binding bindingExchangeQueue32(TopicExchange exchange, Queue queue32) {
//        return BindingBuilder.bind(queue32).to(exchange).with("*.log.#");
//    }

    /**
     *
     * 延迟交换机(x-delayed-type、x-delayed-message固定写法)
     */
    @Bean("delayExchange")
    public CustomExchange delayExchange(){
        Map<String,Object> args = new HashMap<>();
        args.put("x-delayed-type","direct");
        return new CustomExchange("delay.exchange","x-delayed-message",true,true,args);
    }

    @Bean("delayQueue")
    public Queue delayQueue(){
        return new Queue("delay.queue",true,false,false);
    }

    @Bean
    public Binding bindingDelayExchangeQueue(CustomExchange delayExchange, Queue delayQueue){
        return BindingBuilder.bind(delayQueue).to(delayExchange).with("delay.routkey").noargs();
    }
}
